package com.fzkj.nio.channel;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 读取数据nio
 */
public class Read {

    public static void main(String[] args) {
        String path = "src/com/fzkj/nio/channel/data.txt";
        try {
            // 1、创建字节输入流与源文件接通
            FileInputStream fis = new FileInputStream(path);
            // 2、获得管道
            FileChannel channel = fis.getChannel();
            // 3、获得缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            // 4、读取数据到缓冲区
            channel.read(buffer);
            buffer.flip();
            // 6、
            String result = new String (buffer.array(), 0, buffer.remaining());
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
